//
//  CreateCode.m
//  Runtime(字典转模型的实现)
//
//  Created by 刘怀轩 on 2017/8/28.
//  Copyright © 2017年 刘怀轩. All rights reserved.
//

#import "CreateCode.h"

@implementation CreateCode

+ (void)createPropertyCode:(id)info {
    if ([info isKindOfClass:[NSArray class]]) {
        [self showWithDict:info[0]];
    } else {
        [self showWithDict:info];
    }
}

+ (void)showWithDict:(NSDictionary *)dict {
    NSMutableString *codes = [NSMutableString string];
    // 遍历字典
    [dict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull value, BOOL * _Nonnull stop) {
        NSString *code;
        if ([value isKindOfClass:NSClassFromString(@"__NSCFBoolean")]) {
            code = [NSString stringWithFormat:@"@property (nonatomic, assign) BOOL %@;", key];
        }else if ([value isKindOfClass:[NSString class]]) {
            code = [NSString stringWithFormat:@"@property (nonatomic, copy) NSString *%@;", key];
        } else if([value isKindOfClass:[NSArray class]]) {
            code = [NSString stringWithFormat:@"@property (nonatomic, strong) NSArray *%@;", key];
        } else if([value isKindOfClass:[NSDictionary class]]) {
            code = [NSString stringWithFormat:@"@property (nonatomic, strong) NSDictionary *%@;", key];
        }else if([value isKindOfClass:[NSNumber class]]) {
            code = [NSString stringWithFormat:@"@property (nonatomic, assign) NSInteger %@;", key];
        }
        [codes appendString:[NSString stringWithFormat:@"\n%@\n", code]];
    }];
    
    NSLog(@"%@", codes);
}

@end
